<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@include file="/WEB-INF/pages/include/css-resource.jsp"%>
<title>角色管理</title>
<style type="text/css">
.row {
	margin-bottom: 10px;
}
</style>
</head>
<body>
	<%@include file="/WEB-INF/pages/include/header.jsp"%>
	<nav aria-label="breadcrumb">
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a
				href="${pageContext.request.contextPath}/main.html"><i
					class="fas fa-home"></i>主页</a></li>
			<li class="breadcrumb-item"><a
				href="${pageContext.request.contextPath}/app/role/index.html">角色管理</a></li>
			<li class="breadcrumb-item active" aria-current="page">修改角色信息</li>
		</ol>
	</nav>

	<div class="container-fluid">
		<div class="row">
			<div class="col-md-12">
				<form action="${pageContext.request.contextPath}/app/role/save.html"
					method="post">
					<input type="hidden" name="id" value="${id}">
					<div class="form-group row">
						<label for="code" class="col-sm-2 col-form-label">编码</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="code" name="code"
								value="${obj.code}">
						</div>
					</div>
					<div class="form-group row">
						<label for="name" class="col-sm-2 col-form-label">名称</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="name" name="name"
								value="${obj.name}">
						</div>
					</div>
					<div class="form-group row">
						<label for="mnemonic" class="col-sm-2 col-form-label">助记码</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" id="mnemonic"
								name="mnemonic" value="${obj.mnemonic}">
						</div>
					</div>
					<div class="form-group row">
						<label for="disabled" class="col-sm-2 col-form-label">禁用</label>
						<div class="col-sm-10">
							<div class="custom-control custom-checkbox mr-sm-2">
								<input type="checkbox" class="custom-control-input"
									id="disabled" name="disabled" ${obj.disabled?"checked":""}>
								<label class="custom-control-label" for="disabled"></label>
							</div>
						</div>
					</div>
					<div class="form-group row">
						<div class="col-12">
							<hr />
						</div>
					</div>
					<div class="form-group row">
						<label for="disabled" class="col-sm-2 col-form-label">角色权限</label>
						<div class="col-sm-10">
							<c:forEach items="${functions}" var="fg">
								<div class="row">
									<div class="col-12 function-group">
										<div class="custom-control custom-checkbox mr-sm-2">
											<input type="checkbox" class="custom-control-input"
												id="${fg.functionGroupId}" name="function"
												value="${fg.functionGroupId}"
												onchange="selectFunctionGroup('${fg.functionGroupId}')" ${fg.selected?"checked":""}>
											<label class="custom-control-label"
												for="${fg.functionGroupId}"><i
												class="fas fa-th-list"></i>${fg.functionGroupName}</label>
										</div>
									</div>
								</div>
								<div id="F${fg.functionGroupId}" class="row"
									style="padding-left: 20px;">
									<c:forEach items="${fg.functionModules}" var="f">
										<div class="col-md-3 col-sm-2">
											<div class="custom-control custom-checkbox mr-sm-2">
												<input type="checkbox" class="custom-control-input"
													id="${f.id}" name="function" value="${f.id}"
													onchange="selectFunctionModule('${f.id}','${fg.functionGroupId}')" ${f.selected?"checked":""}>
												<label class="custom-control-label" for="${f.id}">${f.name}</label>
											</div>
										</div>
									</c:forEach>
								</div>
							</c:forEach>
						</div>
					</div>

					<div class="form-group row">
						<div class="col-md-6">
							<button type="submit" class="btn btn-primary btn-block">保存</button>
						</div>
						<div class="col-md-3">
							<button type="reset" class="btn btn-danger btn-block">重置</button>
						</div>
						<div class="col-md-3">
							<a class="btn btn-secondary btn-block"
								href="${pageContext.request.contextPath}/app/role/index.html">返回</a>
						</div>
					</div>
				</form>
				<div class='${code==0?"text-success":"text-danger"}'>${message}</div>
			</div>
		</div>
	</div>

	<%@include file="/WEB-INF/pages/include/js-resource.jsp"%>
	<script type="text/javascript">
		function selectFunctionModule(id, fgId) {
			selectFg = false;
			$('#F' + fgId + ' input[type="checkbox"]').each(function() {
				if ($(this).is(":checked")) {
					selectFg = true;
					return;
				}
			});
			if (selectFg) {
				$("#" + fgId).prop('checked', true);
			} else {
				$("#" + fgId).prop('checked', false);
			}
		}
		function selectFunctionGroup(id) {
			if ($("#" + id).is(":checked")) {
				$('#F' + id + ' input[type="checkbox"]').each(function() {
					$(this).prop('checked', true);
				});
			} else {
				$('#F' + id + ' input[type="checkbox"]').each(function() {
					$(this).prop('checked', false);
				});
			}
		}
	</script>
</body>
</html>